// pages/person/warehouse/warehouse.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    listarray:[],
    inputvalue: '',
    show:false,
    name: '',
    phone: '',
    address: '',
    id:'',
    uid:''
  },
  // 调用微信地址
  chooseAddress() {
    let that = this
    wx.chooseAddress({
      success(res) {
        that.setData({
          name: res.userName,
          phone: res.telNumber,
          address: res.provinceName + res.cityName + res.countyName + res.detailInfo
        })
        console.log(res.userName)
        console.log(res.postalCode)
        console.log(res.provinceName)
        console.log(res.cityName)
        console.log(res.countyName)
        console.log(res.detailInfo)
        console.log(res.nationalCode)
        console.log(res.telNumber)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(this.data.show)
    let that = this
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        console.log(res.data.id)
        that.setData({
          uid: res.data.id,
         
        })
        wx.request({
          url: app.globalData.api + "mystock",
          data: {
          uid: res.data.id,
            search:''
          },
          success: function (res) {
            console.log('仓库库存', res)
            let result=res.data.list
            console.log(result)
            that.setData({
            listarray:result
            })
          
          },
        })
      },
    })

  },
//确定收货
  confirm(e){
    let that = this
    let id = e.currentTarget.dataset.id
    wx.request({
      url: app.globalData.api + "mygetrecharge",
      data: {
        uid: that.data.uid,
        id:id
      },
      success: function (res) {
        console.log('收货',res)
        wx.showToast({
          title: res.data.msg,
        })
        that.onLoad()
      },
    })
  },
  //进入云仓
  warehouse(e){
    let id = e.currentTarget.dataset.id
    let type = e.currentTarget.dataset.type
    if(type==0){
      this.setData({
        show:true,
        id: e.currentTarget.dataset.id,
      })
    }else{
      wx.request({
        url: app.globalData.api + "myrechargenouse",
        data: {
          uid: this.data.uid,
          id: id,
          type: 1
        },
        success:(res)=> {
          console.log('云仓', res)
          wx.showToast({
            title: res.data.msg,
          })
          this.onLoad()
        },
      })
    }
  },
  // 私有
  person(){
    console.log('名字',this.data.name)
    if (this.data.name){
      wx.request({
        url: app.globalData.api + "myrechargenouse",
        data: {
          uid: this.data.uid,
          id: this.data.id,
          type: 0,
          username: this.data.name,
          address: this.data.address,
          phone: this.data.phone,
        },
        success: (res) => {
          console.log('云仓', res)
          if(res.data.msg=='成功'){
            this.setData({
              show: false
            })
          }
          wx.showToast({
            title: res.data.msg,
          })
          this.onLoad()
        },
      })
    }else{
      wx.showToast({
        title: '请选择地址',
        icon:'none'
      })
    }
  },
  // 输入触发的事件
  write(e) {
    let that = this
    console.log(e.detail.value)
    wx.getStorage({
      key: 'userinfo',
      success: function (res) {
        console.log(res.data.id)
        that.setData({
          uid: res.data.id,

        })
        wx.request({
          url: app.globalData.api + "mystock",
          data: { 
            uid: res.data.id,
            search: e.detail.value
            },
          success: function (res) {
            console.log(res.data.list)
            let result = res.data.list
            console.log(result)
            that.setData({
              listarray: result,
              inputvalue: ''
            })
          
          },
        })
      },
    })
  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})